<?php
namespace app\common;
use Elasticsearch\ClientBuilder;

class Eldata
{

    protected $client=null;

    public static $indexName=null;

    public static $indexTypeDataStruct=null;

    protected $host=[
        '192.168.17.128:9200',
    ];

    public function __construct()
    {
        if($this->client === null){
            $this->client=ClientBuilder::create()
            ->setHosts($this->hosts)
            ->build();
        }

        if($this->client==null){
            return false;
        }

        $this->createIndex(static::$indexName,static::$indexTypeDataStruct);
    }

    public static function setIndexNameStruct($indexName, $indexTypeDataStruct)
    {
        static::$indexName= $indexName;
        static::$indexTypeDataStruct= $indexTypeDataStruct;
    }

    public function createIndex($indexName,$indexTypeDataStruct)
    {

        if(!isset($indexTypeDataStruct)){
            $indexTypeDataStruct = new \stdClass();
        }
        $params=[
            'index' => $indexName,
            'body' => [
                'settings' => [
                    'number_of_shards' => 0,
                    'number_of_replicas' => 0
                ],
                'mappings' => [$indexTypeDataStruct]
            ],
        ];

        $client->indices()->create($params);
    }

    public function insertDoc($indexName,$indexType,$indexID,$indexBody) //indexID 可以为ip值
    {
        $insert_params= [
            'index' => $indexName,
            'type' => $indexType,
            'id' => $indexID,
            'body' => $indexBody
        ];

        return $this->client->index($insert_params);
    }

    public function selectIndex($index,$type,$id)
    {
        $select_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];

        return $this->client->get($select_params);
    }

    public function searchIndex($index, $type, $match_data)
    {
        $search_params= [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => [
                    'match' => $match_data
                ]
            ]
        ];

        return $this->client->search($search_params);
    }

    public function pageSearch($indexName, $indexType, $docSize, $pageNum)
    {
        $search_params = [
            'scroll' => '20s',
            'size' => $docSize,
            'index' => $indexName,
            'body' => [
                "query" => [
                    "match_all" => new \stdClass()
                ]
            ]
        ];
        $response = $this->client->search($search_params);
        $cur_point = 1;
        while(isset($response['hits']['hits']) && count($response['hits']['hits']) > 0 && $cur_point < $pageNum){
            $scroll_id = $response['_scroll_id'];

            $response = $this->client->scroll([
                "scroll_id" => $scroll_id,
                "scroll" => '20s',
            ]);
            $cur_point++;
        }

        return $response['hits']['hits'];
    }

    public function getDocTimeCount($index, $type, $start_time, $end_time)
    {
        $time_search_params = [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => [
                            'range' => [
                                'time' => [
                                    'gt' => $start_time,
                                    'lt' => $end_time,
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];

        return json_decode($this->client->count($time_search_params))['count'];
    }


    public function getSortDoc($index, $type, $count, $sort, $query, $filter)
    {
        $doc_query = [
            'index' => $index,
            'type' => $type,
            'size' => $count,
            'body' => [
                'filtered' => [
                    'filter' => $filter,
                    'query' => $query
                ],
                'sort' => $sort
            ]
        ];

        return json_decode($this->client->search($doc_query))['hits']['hits'];
    }


    public function getDocCount($index, $type, $match_data)
    {
        $doc_params= [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => [
                    'match' => $match_data
                ]
            ]
        ];

        return json_decode($this->client->count($doc_params))['count'];
    }

    public function getDocCountQ($index, $type, $search_data)
    {
        $doc_params = [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => $search_data
            ]
        ];
        return json_decode($this->client->count($doc_params))['count'];
    }

    public function deleteDoc($index, $type, $id)
    {
        $delete_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];

        return $this->client->delete($delete_params);
    }

    public function getDoc($index, $type, $id)
    {
        $get_params = [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];

        return $this->client->get($get_params);
    }

    public function deleteIndex()
    {
        $delete_params= [
            'index' => $delete_params
        ];

        return $this->client->indices->delete($delete_params);
    }

    public function upsertDoc($index, $type, $id, $upsertParams)
    {
        $upsertDoc_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id,
            'body' => [
                'script' => 'ctx._source.counter += count',
                'params' => [
                    'count' => 1
                ]
            ]
        ];

        return $this->client->update($upsertDoc_params);
    }
}
